package elicecn.u116114;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//根据数据库数据生成HTML表单
public class FormHelper {
	public static Connection createOracleConnection()
			throws ClassNotFoundException, SQLException {
		String driverClass = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@10.239.137.151:1521:oraxgx1";
		String user = "sap";
		String password = "oracle";
		return getConnection(driverClass, url, user, password);
	}

	public static Connection getConnection(String driverClass, String url,
			String user, String password) throws ClassNotFoundException,
			SQLException {
		Class.forName(driverClass);
		return DriverManager.getConnection(url, user, password);
	}

	public static String createHTML() throws ClassNotFoundException,
			SQLException {
		String sqlbiz = "select busi_code, busi_name from NA_CODE_COMP_BUSI";
		String sqlinf = "select pro_code, pro_name, pro_type from NA_CODE_BUSI_INFO where busi_code = '$KW$'";
		String sqlval = "select code_id, pro_cont from NA_CODE_BUSI_ANS where pro_code = '$KW$'";
		StringBuffer buf = new StringBuffer();

		Connection cntbiz = createOracleConnection();
		Statement sttbiz = cntbiz.createStatement();
		ResultSet rsbiz = sttbiz.executeQuery(sqlbiz);
		while (rsbiz.next()) {
			String busiCode = rsbiz.getString("busi_code");
			String busiName = rsbiz.getString("busi_name");
			buf.append("\n<tr id='ts$KW$' style='display: none;' class='svc'>\n"
					.replaceAll("\\$KW\\$", busiCode));
			buf.append("<td colspan='2'>\n");
			buf.append("<table align='center'  class='table' mutiColor='true' highlightRow='true' onClick='clickOnTH();' onMouseOver='overTHColor();' onMouseOut='outTHColor();'>\n");
			buf.append("<tr class='header'><td colspan='2' class='bizTitle'>$KW$</td>\n</tr>\n"
					.replaceAll("\\$KW\\$", busiName));

			Connection cntinf = createOracleConnection();
			Statement sttinf = cntinf.createStatement();
			ResultSet rsinf = sttinf.executeQuery(sqlinf.replaceAll("\\$KW\\$",
					busiCode));
			while (rsinf.next()) {
				String pro_code = rsinf.getString("pro_code");
				String pro_name = rsinf.getString("pro_name");
				String pro_type = rsinf.getString("pro_type");
				buf.append("<tr>\n");
				buf.append("<td width='30%'>$KW$</td>\n<td width='70%'>"
						.replaceAll("\\$KW\\$", pro_name));

				if (pro_type.equals("1")) {// 文本框
					buf.append("<input name='$KW$' parent='$PKW$' desc='$DKW$' value=''/>"
							.replaceAll("\\$DKW\\$", pro_name)
							.replaceAll("\\$KW\\$", pro_code)
							.replaceAll("\\$PKW\\$", busiCode));
				} else if (pro_type.equals("2")) {// 多文本框
					buf.append("<textarea name='$KW$' parent='$PKW$' desc='$DKW$' rows='3' cols='40'></textarea>"
							.replaceAll("\\$KW\\$", pro_code)
							.replaceAll("\\$PKW\\$", busiCode)
							.replaceAll("\\$DKW\\$", pro_name));
				} else if (pro_type.equals("3")) {// 单选按纽
					Connection cntval = createOracleConnection();
					Statement sttval = cntval.createStatement();
					ResultSet rsval = sttval.executeQuery(sqlval.replaceAll(
							"\\$KW\\$", pro_code));
					while (rsval.next()) {
						String code_id = rsval.getString("code_id");
						String pro_cont = rsval.getString("pro_cont");
						buf.append("<input type='radio' name='$KW$' parent='$PKW$' desc='$DKW$' value='$VKW$'/>$TKW$\n"
								.replaceAll("\\$KW\\$", pro_code)
								.replaceAll("\\$PKW\\$", busiCode)
								.replaceAll("\\$DKW\\$", pro_name)
								.replaceAll("\\$VKW\\$", code_id)
								.replaceAll("\\$TKW\\$", pro_cont));
					}
					rsval.close();
					sttval.close();
					cntval.close();
				} else if (pro_type.equals("4")) {// 多选按纽
					Connection cntval = createOracleConnection();
					Statement sttval = cntval.createStatement();
					ResultSet rsval = sttval.executeQuery(sqlval.replaceAll(
							"\\$KW\\$", pro_code));
					while (rsval.next()) {
						String code_id = rsval.getString("code_id");
						String pro_cont = rsval.getString("pro_cont");
						buf.append("<input type='checkbox' name='$KW$' parent='$PKW$' desc='$DKW$' value='$VKW$'/>$TKW$\n"
								.replaceAll("\\$KW\\$", pro_code)
								.replaceAll("\\$PKW\\$", busiCode)
								.replaceAll("\\$DKW\\$", pro_name)
								.replaceAll("\\$VKW\\$", code_id)
								.replaceAll("\\$TKW\\$", pro_cont));
					}
					rsval.close();
					sttval.close();
					cntval.close();
				} else if (pro_type.equals("5")) {// 下拉框
					Connection cntval = createOracleConnection();
					Statement sttval = cntval.createStatement();
					ResultSet rsval = sttval.executeQuery(sqlval.replaceAll(
							"\\$KW\\$", pro_code));
					buf.append("<select name='$KW$' parent='$PKW$' desc='$DKW$'>\n"
							.replaceAll("\\$KW\\$", pro_code)
							.replaceAll("\\$PKW\\$", busiCode)
							.replaceAll("\\$DKW\\$", pro_name));
					while (rsval.next()) {
						String code_id = rsval.getString("code_id");
						String pro_cont = rsval.getString("pro_cont");
						buf.append("<option value='$VKW$'>$TKW$</option>\n"
								.replaceAll("\\$VKW\\$", code_id).replaceAll(
										"\\$TKW\\$", pro_cont));
					}
					buf.append("</select>\n");
					rsval.close();
					sttval.close();
					cntval.close();
				} else if (pro_type.equals("6")) {// 费率
					buf.append("<input name='$KW$' parent='$PKW$' desc='$DKW$' value='' class='currency'/>"
							.replaceAll("\\$KW\\$", pro_code)
							.replaceAll("\\$PKW\\$", busiCode)
							.replaceAll("\\$DKW\\$", pro_name));
				} else if (pro_type.equals("7")) {// 文件
					buf.append("<input type='file' name='$KW$' parent='$PKW$' desc='$DKW$' value='' class='file'/>"
							.replaceAll("\\$KW\\$", pro_code)
							.replaceAll("\\$PKW\\$", busiCode)
							.replaceAll("\\$DKW\\$", pro_name));
				}
				buf.append("</td>\n</tr>\n");
			}
			rsinf.close();
			sttinf.close();
			cntinf.close();

			buf.append("</table></td></tr>\n");
		}
		rsbiz.close();
		sttbiz.close();
		cntbiz.close();
		return buf.toString();
	}

	public static void main(String[] args) throws ClassNotFoundException,
			SQLException {
		System.out.println(createHTML());
	}
}
